<?php

App::uses('AppController', 'Controller');

/**
 * Contents Controller
 *
 * @property Content $Content
 */
class ContentsController extends AppController {

    /**
     * index method
     *
     * @return void
     */    
    public $helpers = array('Js');
    public $paginate = array(
        'Image' => array(
            'limit' => 10            
        ),
        'Content' => array(
            'limit' => 5,
            'order' => array(
                'Content.sort_order' => 'asc'
            )
        )
    );

    public function index($id = null, $page = null) {
        
        $user = $this->Auth->user();
        if ($id == NULL) {
            $content = $this->Content->find('first');
        } else {
            $content = $this->Content->findById($id);
        }

        if ($content['Content']['gallery_id'] != NULL && $content['Content']['gallery_id']!=0) {
            $this->loadModel('Image');
            $this->paginate['Image']['conditions']=array('Image.gallery_id'=>$content['Content']['gallery_id']);
            $images = $this->paginate('Image');
            $this->set('images', $images);
        }
        $this->Content->recursive = 0;
        $this->set('content', $content);
    }

    public function mainmenu() {
        $data = $this->Content->find('all', array('fields' => array('Content.id', 'Content.menu'), 'order' => array('Content.sort_order' => 'asc')));
        return $data;
    }

   

    /**
     * admin_index method
     *
     * @return void
     */
    public function admin_index() {
        //$this->autoRender = false;
        
        $this->Content->recursive = 0;
        $this->set('contents', $this->paginate());
    }

    /**
     * admin_view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_view($id = null) {
        if (!$this->Content->exists($id)) {
            throw new NotFoundException(__('Invalid content'));
        }
        $options = array('conditions' => array('Content.' . $this->Content->primaryKey => $id));
        $this->set('content', $this->Content->find('first', $options));
    }

    /**
     * admin_add method
     *
     * @return void
     */
    public function admin_add() {

        if ($this->request->is('post')) {
            $formData = $this->request->data;
            $myData = $this->Content->find('first', array('fields' => array('MAX(Content.sort_order)+1 AS sort_order', '*')));
            //debug($myData);
            if ($myData['Content']['sort_order'] == NULL) {
                $formData['Content']['sort_order'] = 1;
            } else {
                $formData['Content']['sort_order'] = $myData[0]['sort_order'];
            }
            //debug($formData);

            //$this->Content->create();
            if ($this->Content->Save($formData)) {
                $this->Session->setFlash(__('The content has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Tartalom nem lett mentve. Próbáld meg újra.'));
            }
        }
        $galleries = $this->Content->Gallery->find('list');
        //$galleries[0] = NULL;
        
        $galleries = array_merge(array(0=>'Nincs'),$galleries);
        
        $this->set(compact('galleries'));
    }

    /**
     * admin_edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null) {
        if (!$this->Content->exists($id)) {
            throw new NotFoundException(__('Invalid content'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Content->save($this->request->data)) {
                $this->Session->setFlash(__('A tartalom mentve'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('A tartalom nem lett elmentve. Próbáld meg újra.'));
            }
        } else {
            $options = array('conditions' => array('Content.' . $this->Content->primaryKey => $id));
            $this->request->data = $this->Content->find('first', $options);
        }        
        $galleries = $this->Content->Gallery->find('list');
        $galleries = array_merge(array(0=>'Nincs'),$galleries);
        $this->set(compact('galleries'));
    }

    /**
     * admin_delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null) {
        $content = $this->Content->findById($id);
        $contents = $this->Content->find('all', array(
        'conditions' => array(
        'Content.sort_order > ' => $content['Content']['sort_order']
        )
        )
        );
        
        
        foreach ($contents as $content) {
            $content['Content']['sort_order']-=1;
            $this->Content->save($content);
        }
        
        $this->Content->id = $id;
        if (!$this->Content->exists()) {
            throw new NotFoundException(__('Invalid content'));
        }
        
        
        
        
        $this->request->onlyAllow('post', 'delete');
        if ($this->Content->delete()) {
            $this->Session->setFlash(__('Content deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Content was not deleted'));
        $this->redirect(array('action' => 'index'));
    }

    public function admin_increment($id) {
        $currentSortOrder = $this->Content->findById($id);
        $previousSortOrder = $this->Content->findBySortOrder($currentSortOrder['Content']['sort_order'] + 1);

        //debug($previousSortOrder);
        $current = $currentSortOrder['Content']['sort_order'];
        $previous = $previousSortOrder['Content']['sort_order'];
        //echo $current . ' ez  ' . $previous;

        if ($previousSortOrder != NULL) {
            $dipatcher = $currentSortOrder;

            $currentSortOrder['Content']['sort_order'] = $previousSortOrder['Content']['sort_order'];
            $previousSortOrder['Content']['sort_order'] = $dipatcher['Content']['sort_order'];

            //$currentSortOrder['Content']['sort_order']+=1;
            $this->Content->save($currentSortOrder);
            $this->Content->save($previousSortOrder);
        }



        $this->redirect(array('action' => 'admin_index'));
    }

    public function admin_decrement($id) {
        $currentSortOrder = $this->Content->findById($id);
        $previousSortOrder = $this->Content->findBySortOrder($currentSortOrder['Content']['sort_order'] - 1);
        $current = $currentSortOrder['Content']['sort_order'];
        $previous = $previousSortOrder['Content']['sort_order'];
        if ($previousSortOrder != NULL) {
            $dipatcher = $currentSortOrder;
            $currentSortOrder['Content']['sort_order'] = $previousSortOrder['Content']['sort_order'];
            $previousSortOrder['Content']['sort_order'] = $dipatcher['Content']['sort_order'];
            $this->Content->save($currentSortOrder);
            $this->Content->save($previousSortOrder);
        }
        $this->redirect(array('action' => 'admin_index'));
    }

}
